package com.icss.ah.activity.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.activity.service.UseStatisticService;
import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.util.ResourceLoader;

/**
 * 优惠劵使用统计
 * @author js
 *
 */
@Controller
@Scope("prototype")
public class UseStatisticsAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private final Logger errlog = Logger.getLogger("E");
	
	@Autowired
	private UseStatisticService useStatisticService;
	
	private String cpsName;//优惠卷名称
	private String useDate;//生效时间
	private String cpsId;//优惠Id
	private String shop_Id;//店铺Id
	private String mobile;//电话
	private String user_name;//账号名称
	
	/**
	 * 优惠券使用统计界面
	 * @return
	 */
	public String toUseListPage(){
		String returnPage = "toUseListPage";
		return returnPage;
	}
	
	/**
	 * 加载列表
	 */
	public void queryList(){
		try {
			Map<String, String> map = new HashMap<String, String>();
			String corpId = ResourceLoader.getInstance("icss").getString("corpId"); //从配置文件里取公众号id
			String userCode = getUserCodeFromSession();
			map.put("corpId", corpId);
			map.put("userCode", userCode);
			map.put("cpsName", cpsName);
			map.put("useDate", useDate);
			map.put("startIndex", getStartIndexMysql());
			map.put("endIndex", getEndIndexMysql());
			List<Map<String, Object>> list = useStatisticService.getQueryList(map);
			Long total = useStatisticService.getQueryListCount(map);
			dataGridJson = new EasyuiDataGridJson();
			dataGridJson.setRows(list);
			dataGridJson.setTotal(total);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("UseStatisticsAction|异常：",e);
		} finally {
			str2Resp(JSONObject.fromObject(dataGridJson).toString());
		}
	}

	/**
	 * 跳转明细页面
	 * @return
	 */
	public String detailPage(){
		request = getRequest();
		String returnPage = "toDetailPage";
		request.setAttribute("cpsId", cpsId);
		return returnPage;
	}
	
	/**
	 * 使用优惠券明细
	 */
	public void queryUserDeatailList(){
		try {
			Map<String, String> map = new HashMap<String, String>();
			map.put("yh_Id", cpsId);
			map.put("mobile", mobile);
			map.put("user_name", user_name);
			map.put("startIndex", getStartIndexMysql());
			map.put("endIndex", getEndIndexMysql());
			List<Map<String, Object>> list = useStatisticService.getUserDetail(map);
			Long total = useStatisticService.getUserDetailCount(map);
			dataGridJson = new EasyuiDataGridJson();
			dataGridJson.setRows(list);
			dataGridJson.setTotal(total);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("UseStatisticsAction|异常：",e);
		}finally{
			str2Resp(JSONObject.fromObject(dataGridJson).toString());
		}
	}
	
	public String getCpsName() {
		return cpsName;
	}

	public void setCpsName(String cpsName) {
		this.cpsName = cpsName;
	}

	public String getUseDate() {
		return useDate;
	}

	public void setUseDate(String useDate) {
		this.useDate = useDate;
	}

	public String getCpsId() {
		return cpsId;
	}

	public void setCpsId(String cpsId) {
		this.cpsId = cpsId;
	}

	public String getShop_Id() {
		return shop_Id;
	}

	public void setShop_Id(String shop_Id) {
		this.shop_Id = shop_Id;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getUser_name() {
		return user_name;
	}

	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}

}
